package com.gddb.xwj.mywiki.controller;

import com.gddb.xwj.mywiki.req.DocQueryReq;
import com.gddb.xwj.mywiki.req.DocSaveReq;
import com.gddb.xwj.mywiki.resp.CommonResp;
import com.gddb.xwj.mywiki.resp.DocQueryResp;
import com.gddb.xwj.mywiki.resp.PageResp;
import com.gddb.xwj.mywiki.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：文档管理
 *
 * @author xwj
 * @create 2022-03-18 19:17
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    private DocService docService;

    /**
     * 全量查询 GET请求是form提交方式 方法参数不用加@RequstBody
     * @return
     */
    @GetMapping("/all")
    public CommonResp all(DocQueryReq req) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> list = docService.all(req);
        resp.setContent(list);
        return resp;
    }

    /**
     * 分页查询
     * @param req
     * @return
     */
    @GetMapping("/list")
    public CommonResp list(DocQueryReq req){
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> cont = docService.list(req);
        resp.setContent(cont);
        return resp;
    }

    /**
     * 保存
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq req){
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }

    /**
     * /{id} 用 @PathValiable接收
     * JSON 用 @RequestBody接收
     * @return
     */
    @DeleteMapping("/delete")
    public CommonResp delete(@RequestParam("id") Long id){
        CommonResp<Object> resp = new CommonResp<>();
        docService.delete(id);
        return resp;
    }

    /**
     * 根据一组id 删除
     * @param idsStr
     * @return
     */
    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        List<String> list = Arrays.asList(idsStr.split(","));
        //	List<String>转List<Long>
        List<Long> collect = list.stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        docService.delete(collect);
        return resp;
    }

    /**
     * 根据ID获取富文本内容
     * @return
     */
    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();
        String content = docService.findContent(id);
        resp.setContent(content);
        return resp;
    }


    /**
     *
     * @param ebookId
     * @return
     */
    @GetMapping("/find/{ebookId}")
    public CommonResp findByEbookId(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> list = docService.findByEbookId(ebookId);
        resp.setContent(list);
        return resp;
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp commonResp = new CommonResp();
        docService.vote(id);
        return commonResp;
    }
}
